﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;
using System.IO;
using System.Drawing.Imaging;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M003_USER : FormBase
    {
        #region 定义变量

        /// <summary>
        /// 是已经否初始化
        /// </summary>
        private bool isInit = true;

        /// <summary>
        /// 用户实体
        /// </summary>
        private readonly MOD_M003_USER modUser = null;

        /// <summary>
        /// 业务操作实体
        /// </summary>
        private readonly BLL_M003_USER bllUser = null;

        /// <summary>
        /// 用户
        /// </summary>
        private const string dataName = "用户";

        #endregion

        #region 方法

        /// <summary>
        /// 构造函数
        /// </summary>
        public M003_USER()
        {
            InitializeComponent();
            modUser = new MOD_M003_USER();
            bllUser = new BLL_M003_USER();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
            InitialGridView();
            InitialControl();
            InitialAllCombox();
            isInit = false;
        }

        /// <summary>
        /// 设置Tab顺序
        /// </summary>
        protected void SetControlsIndex()
        {
            grdMain.TabStop = false;
            int intIndex = 0;intIndex++;
            txtUSER_ID.TabIndex = intIndex;intIndex++;
            txtUSER_LOGIN_ID.TabIndex = intIndex;intIndex++;
            txtUSER_NAME.TabIndex = intIndex; intIndex++;
            cmbUSER_GROUP.TabIndex = intIndex; intIndex++;
            txtUSER_PHONE1.TabIndex = intIndex;intIndex++;
            txtUSER_ADDRESS1.TabIndex = intIndex;intIndex++;
            txtMEMO.TabIndex = intIndex;intIndex++;
            btnAdd.TabIndex = intIndex; intIndex++;
            btnExit.TabIndex = intIndex;
        }

        /// <summary>
        /// Grid初始化
        /// </summary>
        private void InitialGridView()
        {
            grdMain.AllowUserToAddRows = false;
            grdMain.AllowUserToResizeColumns = true;
            grdMain.AllowUserToDeleteRows = false;
            grdMain.AllowUserToResizeRows = false;
            grdMain.ColumnHeadersHeight = 30;
            grdMain.ColumnHeadersVisible = true;
            grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Raised;
            grdMain.ReadOnly = true;
            grdMain.RowHeadersVisible = false;
            grdMain.RowTemplate.Resizable = DataGridViewTriState.False;
            grdMain.RowTemplate.Height = 25;
            grdMain.RowTemplate.ReadOnly = true;
            grdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdMain.ScrollBars = ScrollBars.Both;
            grdMain.TabStop = false;

            grdMain.Columns.Add("NO", "NO.");
            grdMain.Columns["NO"].Width = 45;
            grdMain.Columns["NO"].Frozen = true;
            grdMain.Columns["NO"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.Columns["NO"].Resizable = DataGridViewTriState.False;

            grdMain.Columns.Add("USER_ID", "编号");
            grdMain.Columns["USER_ID"].Width = 140;
            grdMain.Columns["USER_ID"].Frozen = true;
            grdMain.Columns["USER_ID"].Resizable = DataGridViewTriState.False;

            grdMain.Columns.Add("USER_LOGIN_ID", "登录ID");
            grdMain.Columns.Add("USER_NAME", "姓名");
            grdMain.Columns.Add("USER_GROUP", "组别");
            grdMain.Columns.Add("USER_GROUP_HIDE", "");
            grdMain.Columns.Add("USER_STATE", "状态");
            grdMain.Columns["USER_STATE"].Frozen = true;
            grdMain.Columns.Add("USER_STATE_HIDE", "");
            grdMain.Columns.Add("USER_SEX", "性别");
            grdMain.Columns["USER_SEX"].Frozen = true;
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn() { Text = "删除", Width = 80, HeaderText = "删除", SortMode = DataGridViewColumnSortMode.NotSortable, Resizable = DataGridViewTriState.False, Frozen = true };
            grdMain.Columns.Add(btnDelete);
            grdMain.Columns["USER_SEX"].Resizable = DataGridViewTriState.False;

            grdMain.Columns.Add("USER_SEX_HIDE", "");
            grdMain.Columns.Add("USER_BIRTHDAY", "出生年月日");
            grdMain.Columns.Add("USER_PHONE1", "电话");
            grdMain.Columns.Add("USER_PHONE2", "");
            grdMain.Columns.Add("USER_ADDRESS1", "住址");
            grdMain.Columns.Add("USER_ADDRESS2", "");
            grdMain.Columns.Add("USER_EMAIL", "E_Mail");
            grdMain.Columns.Add("USER_POST", "邮编");
            grdMain.Columns.Add("MEMO", "备注");
            grdMain.Columns.Add("USER_WD_HIDE", "");
            
            
            grdMain.Columns["USER_LOGIN_ID"].Width = 120;
            grdMain.Columns["USER_NAME"].Width = 110;
            grdMain.Columns["USER_GROUP"].Width = 110;
            grdMain.Columns["USER_GROUP_HIDE"].Width = 0;
            grdMain.Columns["USER_STATE"].Width = 80;
            grdMain.Columns["USER_STATE_HIDE"].Width = 0;
            grdMain.Columns["USER_SEX"].Width = 80;
            grdMain.Columns["USER_SEX_HIDE"].Width = 0;
            grdMain.Columns["USER_BIRTHDAY"].Width = 120;
            grdMain.Columns["USER_PHONE1"].Width = 100;
            grdMain.Columns["USER_PHONE2"].Width = 0;
            grdMain.Columns["USER_ADDRESS1"].Width = 150;
            grdMain.Columns["USER_ADDRESS2"].Width = 0;
            grdMain.Columns["USER_EMAIL"].Width = 100;
            grdMain.Columns["USER_POST"].Width = 80;
            grdMain.Columns["MEMO"].Width = 200;
            grdMain.Columns["USER_WD_HIDE"].Width = 0;
            grdMain.Columns["USER_GROUP_HIDE"].Visible = false;
            grdMain.Columns["USER_STATE_HIDE"].Visible = false;
            grdMain.Columns["USER_SEX_HIDE"].Visible = false;
            grdMain.Columns["USER_WD_HIDE"].Visible = false;
            grdMain.Columns["USER_PHONE2"].Visible = false;
            grdMain.Columns["USER_ADDRESS2"].Visible = false;
            
        }

        /// <summary>
        /// 控件清空
        /// </summary>
        private void InitialControl()
        {
            txtUSER_ID.Text = "";
            txtUSER_LOGIN_ID.Text = "";
            txtUSER_NAME.Text = "";
            cmbUSER_GROUP.Text = "";
            cmbUSER_GROUP.DropDownStyle = ComboBoxStyle.DropDownList;
            txtUSER_PHONE1.Text = "";
            ckbM.Checked = true;
            ckbF.Checked = true;
            ckbEnable.Checked = true;
            ckbDisEnable.Checked = false;
            txtUSER_ADDRESS1.Text = "";
            txtMEMO.Text = "";
        }

        /// <summary>
        /// 绑定Grid数据
        /// </summary>
        private void BindGridData()
        {
            if (isInit)
            {
                return;
            }
            grdMain.Rows.Clear();            
            ReturnValue returnValue;
            modUser.ClareValue();
            modUser.USER_ID = this.txtUSER_ID.Text.Trim();
            modUser.USER_LOGIN_ID = this.txtUSER_LOGIN_ID.Text.Trim();
            modUser.USER_NAME = this.txtUSER_NAME.Text.Trim();
            modUser.USER_PHONE1 = this.txtUSER_PHONE1.Text.Trim();
            modUser.USER_ADDRESS1 = this.txtUSER_ADDRESS1.Text.Trim();
            modUser.USER_GROUP = this.cmbUSER_GROUP.SelectedValue.ToString();
            modUser.MEMO = this.txtMEMO.Text.Trim();
            //用户性别
            if (ckbM.Checked && ckbF.Checked)
            {
                modUser.USER_SEX = "";
            }
            else if (ckbM.Checked)
            {
                modUser.USER_SEX = "0";
            }
            else if (ckbF.Checked)
            {
                modUser.USER_SEX = "1";
            }
            else
            {
                modUser.USER_SEX = "3";
            }
            //用户状态
            if (ckbEnable.Checked && ckbDisEnable.Checked)
            {
                modUser.USER_STATE = "";
            }
            else if (ckbEnable.Checked)
            {
                modUser.USER_STATE = "0";
            }
            else if (ckbDisEnable.Checked)
            {
                modUser.USER_STATE = "1";
            }
            else
            {
                modUser.USER_STATE = "3";
            }
            this.Cursor = Cursors.WaitCursor;
            try
            {
                returnValue = bllUser.GetUserList(modUser);
                if (!returnValue.IsSuccess)
                {
                    Method.MessageShow(returnValue.ErrorID,dataName);
                    this.Cursor = Cursors.Default;
                    return;
                }
                grpList.Text = "一览 共" + returnValue.ResultDataSet.Tables[0].Rows.Count.ToString() + "件";
                for (int i = 0; i < returnValue.ResultDataSet.Tables[0].Rows.Count; i++)
                {
                    grdMain.Rows.Add((i + 1).ToString(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_ID"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_LOGIN_ID"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_NAME"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["GROUP_NM"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_GROUP"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_STATE"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_STATE_HIDE"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_SEX"].ToString().Trim(),
                        "删除",
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_SEX_HIDE"].ToString().Trim(),
                        Method.TimeFormat(returnValue.ResultDataSet.Tables[0].Rows[i]["USER_BIRTHDAY"].ToString()),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_PHONE1"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_PHONE2"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_ADDRESS1"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_ADDRESS2"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_EMAIL"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_POST"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["MEMO"].ToString().Trim(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["USER_WD"].ToString().Trim());
                }
            }
            catch
            {
                Method.MessageShow("E11004",dataName);//An unknown error process!
                this.Cursor = Cursors.Default;
            }
            this.Cursor = Cursors.Default;
        }        

        /// <summary>
        /// 绑定下拉列表框数据
        /// </summary>
        private void InitialAllCombox()
        {
            Method.CmbDataBound("User_Group_ID", cmbUSER_GROUP);
            this.cmbUSER_GROUP.SelectedIndex = 0;
        }

        /// <summary>
        /// 检查用户是否正在使用
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns>true:使用或发生错误 false:未使用</returns>
        protected bool IsUserUsed(string UserID)
        {
            ReturnValue rvReturnValue = bllUser.CheckUserUsed(UserID);
            if (rvReturnValue.IsSuccess)
            {
                if (rvReturnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    Method.MessageShow("W11D01", dataName);//本@正在使用!不能删除!
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                Method.MessageShow(rvReturnValue.ErrorID, dataName);
                return true;
            }
        } 

        #endregion

        #region 事件

        /// <summary>
        /// M003_USER_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M003_USER_Load(object sender, EventArgs e)
        {
            Initial();
            BindGridData();
        }        

        /// <summary>
        /// btnExit_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }

        /// <summary>
        /// M003_USER_KeyUp
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M003_USER_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnExit_Click(sender, e);
                    break;
            }
        }
        
        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (new M003UserEdit().ShowDialog() == DialogResult.OK)
            {
                BindGridData();
            }
        }

        /// <summary>
        /// 查询条件改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void QueryChanged(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            BindGridData();
        }

        /// <summary>
        /// 单击删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            int intColumnIndex = e.ColumnIndex;
            //删除
            if (intColumnIndex == 9 && e.RowIndex != -1)
            {
                ReturnValue returnValue;
                //检查该用户是否正在被使用
                if (IsUserUsed(grdMain.CurrentRow.Cells["USER_ID"].Value.ToString()))
                {
                    this.Cursor = Cursors.Default;
                    return;
                }
                //物理彻底删除
                if (Method.MessageShow("Q22007", dataName) == DialogResult.Yes)//确定要删除该条数据吗?
                {
                    returnValue = bllUser.DelUser(grdMain.CurrentRow.Cells["USER_ID"].Value.ToString());
                    if (!returnValue.IsSuccess)
                    {
                        Method.MessageShow(returnValue.ErrorID, dataName);
                        this.Cursor = Cursors.Default;
                        return;
                    }
                    InitialControl();
                    BindGridData();
                }
            }
        }

        /// <summary>
        /// 双击编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            if (0 == grdMain.Rows.Count || e.RowIndex == -1)
            {
                return;
            }
            modUser.UPDATER_ID = GlobalVal.gloUserInfo.USER_ID;
            modUser.TERMINAL_CD = GlobalVal.gloUserInfo.TERMINAL_CD;
            modUser.USER_ID= grdMain.CurrentRow.Cells["USER_ID"].Value.ToString();
            modUser.USER_LOGIN_ID = grdMain.CurrentRow.Cells["USER_LOGIN_ID"].Value.ToString();
            modUser.USER_WD = grdMain.CurrentRow.Cells["USER_WD_HIDE"].Value.ToString();
            modUser.USER_NAME = grdMain.CurrentRow.Cells["USER_NAME"].Value.ToString();
            modUser.USER_SEX = grdMain.CurrentRow.Cells["USER_SEX_HIDE"].Value.ToString();
            modUser.USER_BIRTHDAY = grdMain.CurrentRow.Cells["USER_BIRTHDAY"].Value.ToString();
            modUser.USER_PHONE1 = grdMain.CurrentRow.Cells["USER_PHONE1"].Value.ToString();
            modUser.USER_PHONE2 = grdMain.CurrentRow.Cells["USER_PHONE2"].Value.ToString();
            modUser.USER_ADDRESS1 = grdMain.CurrentRow.Cells["USER_ADDRESS1"].Value.ToString();
            modUser.USER_ADDRESS2 = grdMain.CurrentRow.Cells["USER_ADDRESS2"].Value.ToString();
            modUser.USER_EMAIL = grdMain.CurrentRow.Cells["USER_EMAIL"].Value.ToString();
            modUser.USER_POST = grdMain.CurrentRow.Cells["USER_POST"].Value.ToString();
            modUser.USER_GROUP = grdMain.CurrentRow.Cells["USER_GROUP_HIDE"].Value.ToString();
            modUser.USER_STATE = grdMain.CurrentRow.Cells["USER_STATE_HIDE"].Value.ToString();
            modUser.MEMO = grdMain.CurrentRow.Cells["MEMO"].Value.ToString();
            if (new M003UserEdit(modUser).ShowDialog() == DialogResult.OK)
            {
                //刷新Grid数据
                BindGridData();
            }
        }

        #endregion
    }
}